/**
 * @author:fangxiaohua
*  @邮箱：abc2710712@qq.com
*  @qq:1295168875
*   用户业务操作操作
*/
void(function(exports){
	//数据库操作
	var dbLib = require('../lib/dbLib');
	//工具类模块
	var tools = require('../lib/toolsLib');
	
	//用户列表
	exports.list = function(req, res){
		var pageNo=req.body.pageNo;
		var pageSize=req.body.pageSize;
		var q=req.body.q;
		var  sql=["select c.title , c.time,c.status,c.id,u.account from t_public c inner join t_user u on c.usr_id=u.id   where c.content like ? or c.title like ? order by time desc "].join("");
		q=q==null?"%%":"%"+q+"%";
		var params=[q,q];
		dbLib.dbPageQuery(sql,pageNo,pageSize,params,function(err,result){
			for(var i in result.records){
				if(result.records[i].status=="1"){
					result.records[i].status="公告发布";
				}else{
					result.records[i].status="公告关闭";
				}
			}
			res.send(result);
			return;
		});
	};
	
	//公告查看
	exports.view=function(req,res){
		var  sql=["select c.title , c.time,c.status,c.id,u.account,c.content from t_public c inner join t_user u on c.usr_id=u.id   where c.id=? "].join("");
		var id=req.body.id;
		dbLib.dbQuery(sql,[id],function(err,result){
			result[0].status=result[0].status=="1"?"公告发布":"公告关闭";
			res.send(result[0]);
			return;
		});
	};
	
	//用户修改
	exports.edit=function(req,res){
		var id=req.body.id;
		var status=req.body.status=="公告发布"?"1":"0";
		var params=[status,id];
		sql="update t_public  set status=?  where id=?";
		dbLib.dbUpdate(sql,params,function(err,result){
			if(err){
				res.send("false");
				return;
			}
			res.send("true");
			return;
		});
	}
	
	//公告发布
	exports.add=function(req,res){
		var usrId=req.session.user.id;
		var title=req.body.title;
		var content=req.body.content;
		var status=req.body.status;
		var params=[usrId,title,content,status];
		sql="insert into t_public (usr_id,title,content,time,status) values(?,?,?,now(),?)";
		dbLib.dbAdd(sql,params,function(err,result){
			if(err){
				res.send("false");
				return;
			}
			res.send("true");
			return;
		});
	};
	
	//首页公告列表
	exports.plist=function(req,res){
		var size=req.body.size;
		var sql="select p.id,p.title,p.time,u.avatar,u.name from t_public p inner join t_user u on p.usr_id=u.id where p.status=1 limit 0,"+size;
		dbLib.dbQuery(sql,[],function(err,result){
			res.send(result);
			return;
		});
	}
})( (function(){
    if(typeof exports === 'undefined') {
        window.exports = {};
        return window.exports;
    } else {
        return exports;
    }
})());